﻿//Copyright 2010 http://tv4home.codeplex.com
//This file is part of TV4Home.Client.TVViewer.
//TV4Home.Client.TVViewer is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
//TV4Home.Client.TVViewer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//You should have received a copy of the GNU Lesser General Public License along with TV4Home.Client.TVViewer. If not, see http://www.gnu.org/licenses/.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Media.Animation;

namespace TV4Home.Client.Desktop.TVViewer.Controls
{
    /// <summary>
    /// Interaktionslogik für WorkingIndicator.xaml
    /// </summary>
    public partial class WorkingIndicator
         : UserControl
    {
        public static readonly DependencyProperty IsWorkingProperty =
            DependencyProperty.Register("IsWorking", typeof(bool), typeof(WorkingIndicator),
            new PropertyMetadata(false, new PropertyChangedCallback(OnIsWorkingChanged)));

        private Storyboard _workingStoryboard;

        public WorkingIndicator()
        {
            InitializeComponent();
            _workingStoryboard = this.Resources["WorkingStoryboard"] as Storyboard;
        }

        #region Properties

        [Category("Common Properties")]
        public bool IsWorking
        {
            get { return Convert.ToBoolean(GetValue(IsWorkingProperty)); }
            set { SetValue(IsWorkingProperty, value); }
        }

        #endregion

        #region Handlers

        private static void OnIsWorkingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((WorkingIndicator)d).UpdateIsWorking(Convert.ToBoolean(e.NewValue));
        }

        private void UpdateIsWorking(bool value)
        {
            this.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
            if (value)
            {
                this.LayoutRoot.Visibility = Visibility.Visible;
                this._workingStoryboard.Begin();
            }
            else
            {
                this.LayoutRoot.Visibility = Visibility.Collapsed;
                this._workingStoryboard.Stop();
            }
        }

        #endregion

    }
}
